package com.http;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.StringTokenizer;
import HTTPSendIntegrator.SendReq;

public class GoOutMM {
	SendReq sendMsg;
	
	public GoOutMM(SendReq sendMsg){
		this.sendMsg = sendMsg;
	}
	
	public boolean SendMM(){
		BufferedWriter outToServer;
		BufferedReader inFromServer;
		
		try {
			Socket socket = new Socket("210.107.196.185", 9000);
			outToServer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
			
			outToServer.write("POST" + " " + "/" + " " + "HTTP/1.1" + "\r\n");
			outToServer.write("Host: " + "210.107.196.185:9000" + "\r\n");
			outToServer.write("Content-Type: " + "application/vnd.wap.mms-message" + "\r\n");
			outToServer.write("Content-Length: " + (sendMsg.toString().length()+2) + "\r\n\r\n");
			outToServer.write(sendMsg.toString());
			outToServer.flush();

		     // Get response
			
		    inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));
		    String line = inFromServer.readLine();
		    System.out.println(line);
		    line = inFromServer.readLine();
		    System.out.println(line);
		    line = inFromServer.readLine();
		    System.out.println(line);
		    
		    StringTokenizer tokenizedLine = new StringTokenizer(line); 
		    tokenizedLine.nextToken();
		    String strContentLength = tokenizedLine.nextToken();
		    int contentLength = Integer.parseInt(strContentLength);
		    char arr[] = new char[contentLength/2];
		    inFromServer.read(arr);
		    System.out.println(new String(arr));
		    return true;
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return false;
		}
	}
	
}
